<?php

       $code = $_GET['code'];
        if(!empty($code)){
                $appinfo = get_auth($code);
		$openid = $appinfo['openid'];
                //if(isset($appinfo['errcode']) && $appinfo['errcode'] == '40029'){
			$url  = sprintf("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx91f7f41fce0885bc&redirect_uri=%s&response_type=code&scope=snsapi_base&state=2&openid=%s#wechat_redirect",
					urlencode(sprintf("http://tianshihui.eventool.cn/pay/weixin/demo2/demo/js_api_call.php?openid=%s&trade_type=JSAP&stamp=%s",$openid,uniqid())),$openid);
			
                        Header($url);
                        exit(0);
                //}
	
        }else{
	
		$url = sprintf("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx91f7f41fce0885bc&redirect_uri=%s&response_type=code&scope=snsapi_base&state=2#wechat_redirect",urlencode("http://tianshihui.eventool.cn/pay/weixin/weixin.php"));
				//'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
                 Header($url);
                exit(0);
        }

        $openid = isset($appinfo['openid']) ? $appinfo['openid'] : '';
//	echo($openid);

        //微信授权
        function get_auth($code){
                $appinfo = array();
                if(!empty($code)){
                        $url1 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx91f7f41fce0885bc&secret=c01d141f71475f83748bffeb2f01c50e&code=".$code."&grant_type=authorization_code";

                        $curl = curl_init(); // 启动一个CURL会话
                        curl_setopt($curl, CURLOPT_URL, $url1); // 要访问的地址
                        //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
                        //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
                        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
                        //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
                        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
                        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
                        $tmpInfo = curl_exec($curl); // 执行操作

                        curl_close($curl); // 关闭CURL会话
                        $appinfo = json_decode($tmpInfo,true);
                }
                return $appinfo;
        }

?>
